package com.jpush.gateway.config;

import com.jpush.gateway.pojo.UserLoginReqDTO;
import com.jpush.gateway.pojo.UserLoginRespDTO;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.web.server.authentication.ServerAuthenticationConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import reactor.core.publisher.Mono;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SpringSecurityTemplate {

    private final List<HttpMessageReader<?>> messageReaders;

    public ReactiveAuthenticationManager roleTxnManager() {
        return authentication -> {
            UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
            return Mono.just(token);
        };
    }


    public SpringSecurityTemplate(ServerCodecConfigurer serverCodecConfigurer) {
        this.messageReaders = serverCodecConfigurer.getReaders();
    }

    public ServerAuthenticationConverter loginConverter() {
        return exchange -> {
            ServerRequest serverRequest = ServerRequest.create(exchange,
                    messageReaders);
            return serverRequest.bodyToMono(UserLoginReqDTO.class)
                    .flatMap(info -> {
                                return loginCheck(info).flatMap(user -> {
                                    Integer userNo = 1;
                                    Integer orgNo = 1;
                                    String name = "xiaosu";
                                    //返回登录成功的token
                                    UsernamePasswordAuthenticationToken authenticationToken =
                                            new UsernamePasswordAuthenticationToken(name, null, new ArrayList<>());
                                    Map<String,Object> jwtPlayload = new HashMap<>();
                                    jwtPlayload.put("userNo",userNo);
                                    jwtPlayload.put("orgNo",orgNo);
                                    jwtPlayload.put("name",name);
                                    authenticationToken.setDetails(jwtPlayload);
                                    return Mono.just(authenticationToken);
                                });
                            }
                    );
        };
    }

    private Mono<UserLoginRespDTO> loginCheck(UserLoginReqDTO loginInfo){

        // 根据提交的用户名和密码，判断是否存在这个用户
        UserLoginRespDTO respDTO = new UserLoginRespDTO();
        respDTO.setOrgNo(1);
        respDTO.setUserNo(1);
        respDTO.setUserName("xiaosu");
        return Mono.just(respDTO);
    }
}
